package com.zzptc.syl.traffic;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Administration extends AppCompatActivity implements View.OnClickListener {

    private EditText etchongzhi;
    private Button btchongzhi;
    private Button chaxun;
    private String carunm;
    private String amoun;
    private String cards[] = new String[]{"辽A10001","辽A10002","辽A10003","辽A10004","辽A10005",};
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    private TextView balanceText;
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    Carservice carservice =retrofit.create(Carservice.class);
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.administration);
        initview();
        balanceText = findViewById(R.id.balanceText);
        spinner =(Spinner)findViewById(R.id.sp_1);
        String[] arr=new String[]{"1","2","3","4","5"};
        adapter=new ArrayAdapter(this,R.layout.xiala,arr);
        //adapter.setDropDownViewResource(R.layout.xiala);
        spinner.setDropDownVerticalOffset(100);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                String name=adapter.getItem((int) l).toString();
//                Toast.makeText(MainActivity.this, "你的选择是"+name, Toast.LENGTH_SHORT).show();
                carunm = cards[(int) l];
                queryRecharge(carunm,"0");
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
    public void queryRecharge(String carno, String money){
        Map<String,String> map=new HashMap<>();
        map.put("who","");
        map.put("carno",carno);
        map.put("amount",money);
        RequestBody body=RequestBody.create(MediaType.parse("application/json;charset=utf_8"),new Gson().toJson(map));
        Call<Car> car= carservice.getCar(map);
        car.enqueue(new Callback<Car>() {
            @Override
            public void onResponse(Call<Car> call, Response<Car> response) {
//                Toast.makeText(context, "充值成功", Toast.LENGTH_SHORT).show();
                //响应的内容
//                if (response.body() != null) {
                balanceText.setText(String.valueOf(response.body().getData().get(0).getAmount()));
//                }else {
//                    throw new RuntimeException("response body is null");
//                }
            }

            @Override
            public void onFailure(Call<Car> call, Throwable t) {
            }
        });
    }
    private void initview() {
        etchongzhi=findViewById(R.id.et_chongzhi);
        btchongzhi=findViewById(R.id.Bt_chongzhi);
        chaxun=findViewById(R.id.Bt_chaxun);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Bt_chaxun:
                queryRecharge(carunm, "0");
                break;
            case R.id.Bt_chongzhi:
                Toast.makeText(this, "充值成功", Toast.LENGTH_SHORT).show();
                queryRecharge(carunm, etchongzhi.getText().toString());
                break;
            default:
        }
    }

}